<?php

class InfoController extends UserLoginBaseC {

    public function accessRules() {
        
    }

    //会员资料
    public function actionIndex() {
        $uid = Yii::app()->user->id;
        $MenberModel = new MembersModel();
        //$MembersAddressModelStd = new MembersAddressModel();
        if (IS_POST) {
            $model = $MenberModel->findByPk($uid);
            if (empty($_POST['username'])) {
                $this->error('请输入用户名！');
            }
            $moblie = trim($_POST['phone']);
            if (empty($moblie)) {
                $this->error('手机号不能为空！');
            }
            if (empty($_POST['sex'])) {
                $this->error('请选择性别！');
            }
            if (empty($_POST['Date'])) {
                $this->error('请选择生日！');
            }
            //手机号需要唯一
            if ($moblie != $model->phone && MembersModel::getByPhone($moblie)) {
                $this->error('该手机已经被注册！');
            }
            //短信验证码
/*            $snscode = $_POST['snscode'];
            if ($moblie != $model->phone && SnsVerification::getInstance()->verification($snscode, $moblie, 'cellphone') !== true) {
                $this->error('短信验证码错误！');
            }*/
            $model->sex = $_POST['sex'];
            $dateTime = strtotime($_POST['Date']);
            $model->year = date('Y', $dateTime);
            $model->month = date('m', $dateTime);
            $model->day = date('d', $dateTime);
            $model->phone = $moblie;
            if ($model->validate() && $model->save()) {
                $this->success('保存成功！', self::U('user/info/index'));
            } else {
                $this->error($model->getOneError() ? $model->getOneError() : '信息更新失败！');
            }
            exit;
        }
        //邀请好友列表
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $limit = 20;
        $model = MembersInviteModel::model();
        $where = array(
            'uid' => Yii::app()->user->id,
            'order' => 'id desc',
        );
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId, array(
                    'tpl' => '{prev}{liststart}{list}{listend}{next}',
                    'prev' => '<',
                    'next' => '>',
        ));
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $data = $model->findAll($criteria);

        //var_dump($data);
        //var_dump($MenberModel->findByPk($uid));
        $userInfo = $MenberModel->findByPk($uid);
        $this->assign("page", $page->show());
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('memberInfo', $userInfo);
        $this->render('index', array(
            'user' => $userInfo
        ));
    }

    public function actionAliWang() {

        $MembersTaobaoModel = new MembersTaobaoModel();

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'tb-form') {
            echo CActiveForm::validate($memberTB);
            Yii::app()->end();
        }
        if (isset($_POST['MembersTaobaoModel'])) {
            $MembersTaobaoModel->attributes = $_POST['MembersTaobaoModel'];
            $MembersTaobaoModel->uid = Yii::app()->user->id;
            $MembersTaobaoModel->add_time = time();
            // validate user input and redirect to the previous page if valid
            if ($MembersTaobaoModel->validate()) {
                $MembersTaobaoModel->save();
            }
        }
        $memberTB = $MembersTaobaoModel->getMemberTB(Yii::app()->user->id);
        $this->render('aliwang', array('model' => $memberTB, 'MembersTaobaoModel' => $MembersTaobaoModel));
    }

    public function actionAddress() {
        $MembersAddressModelStd = new MembersAddressModel();
        $MembersAddressModel = $MembersAddressModelStd->getMemberAddres(Yii::app()->user->id);
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'address-form') {
            echo CActiveForm::validate($memberTB);
            Yii::app()->end();
        }
        if (isset($_POST['MembersAddressModel'])) {
            $MembersAddressModel->uid = Yii::app()->user->id;
            $MembersAddressModel->attributes = $_POST['MembersAddressModel'];
            // validate user input and redirect to the previous page if valid
            if ($MembersAddressModel->validate()) {
                $MembersAddressModel->save();
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }


        $this->render('address', array('memberAddress' => $MembersAddressModel));
    }

    public function actionGetRegions() {
        $pid = (int) $_GET['pid'] ? $_GET['pid'] : 0;
        $region_type = (int) $_GET['type'] ? $_GET['type'] : 0;
        echo CHtml::tag('option', array('value' => 0), '-请选择-', true);
        $data = RegionModel::model()->findAll('region_type=:type and parent_id = :pid', array(':type' => $region_type, ":pid" => $pid));
        foreach ($data as $item) {
            echo CHtml::tag('option', array('value' => $item->region_id), CHtml::encode($item->region_name), true);
        }
        exit;
    }

    //绑定支付宝
    public function actionAlipay() {
        $model = MembersModel::model();
        $data = $model->findByPk(Yii::app()->user->id);
        if (IS_POST) {
            $alipay = Yii::app()->request->getParam('alipay');
            if (empty($alipay)) {
                $this->error('支付宝帐号不能为空！');
            }
            $alipay_name = Yii::app()->request->getParam('alipay_name');
            if (empty($alipay_name)) {
                $this->error('姓名不能为空！');
            }
            $data->alipay = trim($alipay);
            $data->alipay_name = trim($alipay_name);
            if ($data->validate() && $data->save()) {
                $this->success('修改成功！', self::U('alipay'));
            } else {
                $this->error('修改失败！');
            }
        } else {
            $this->assign('data', $data->attributes);
            $this->render();
        }
    }

    /**
     * 密码修改
     * @return [type] [description]
     */
    public function actionPassword() {
        $objModel = MembersModel::model()->findByPk(yii::app()->user->id);
        if (!$objModel) {
            $this->error('找不到此记录', 'index');
        }
        if (IS_POST) {
            if (empty($_POST['password'])) {
                $this->error('请输入原密码');
            }
            if ($_POST['password_1'] != $_POST['password_2']) {
                $this->error('两次密码不一致');
            }
            $info = Yii::app()->user->getLocalUser(Yii::app()->user->username, trim($_POST['password']));
            if (empty($info)) {
                $this->error('原密码不正确');
            }
            $objModel->password = trim($_POST['password_1']);
            if ($objModel->validate()) {
                $objModel->password = $objModel->hashPassword($_POST['password_1']);
                $objModel->save(true, array('password'));
                $this->success('密码修改成功', self::U('index'));
            } else {
                $error = $objModel->getOneError();
                $this->error($error ? $error : '修改失败！');
            }
        }
        $this->render('password', array('model' => $objModel));
    }

    /**
     * 解绑
     * @return [type] [description]
     */
    public function actionUnbund() {
        $id = Yii::app()->request->getParam('id');
        if (!$id) {
            $this->error('参数有误');
        }

        $params = array(
            'id' => $id,
            'uid' => yii::app()->user->id,
        );
        $objModel = MembersOauthModel::model()->find('id = :id AND uid = :uid', $params);

        if (!$objModel) {
            $this->error('找不到对应记录');
        }

        $objModel->delete();
        $this->success('解绑成功');
    }

    /**
     * 绑定列表
     * @return [type] [description]
     */
    public function actionBund() {

        $arrBundList = MembersOauthModel::model()->getBundList(yii::app()->user->id);
        $this->assign('data', $arrBundList);
        $this->render('bund');
    }

    /**
     * 从买家转为卖家
     * @return [type] [description]
     */
    public function actionTransfer() {

        $objMemberModel = MembersModel::model()->findByPk(yii::app()->user->uid);
        if ($objMemberModel->kind == 0) {
            $this->error('您已经是商家用户');
        }

        //$objModel = MembersTransfer::model()->findByPk(yii::app()->user->uid, "status = 0");
        $objModel = MembersTransfer::model()->findByPk(yii::app()->user->uid);
        if ($objModel && $objModel->status != 0) {
            $this->error('您的提交已经处理过');
        }
        empty($objModel) ? $objModel = new MembersTransfer() : '';

        $objTaobaoModel = MembersTaobaoModel::model()->getMemberTB(yii::app()->user->uid);
        $this->assign('userinfo', $objMemberModel);
        $this->assign('usertaobao', $objTaobaoModel);
        if ($_POST['MembersTransfer']) {
            if ($objModel->editor($_POST['MembersTransfer'])) {
                //通知对应的后台管理人员
                $extendParams = array(
                    'title' => '用户' . Yii::app()->user->username . ' 申请转换为商家会员！',
                    'content' => '用户' . Yii::app()->user->username . ' 申请转换为商家会员！请及时处理！',
                );
                $notice_transfer = (int) ConfigModel::model()->getConfig('notice_transfer');
                if ($notice_transfer) {
                    AdminMessage::model()->notice($notice_transfer, false, AdminMessage::transfer, $extendParams);
                }
                $this->success('提交成功,请等待处理');
            }
        }
        $this->render('transfer', array('model' => $objModel));
    }

}
